<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="所属区域" prop="pid">
				<el-select v-model="form.pid"  filterable style="width: 100%" clearable>
					<el-option v-for="item in cat" :key="item.id" :label="item.name" :value="item.id"/>
				</el-select>
			</el-form-item>
            <el-form-item label="桌号" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
            <el-form-item label="最大人数" prop="sort">
				<el-input-number v-model="form.max_num" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="0" style="width: 100%;" placeholder="数字越大 越靠前"></el-input-number>
			</el-form-item>
			<el-form-item label="显示" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
 
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
                cat:[],
				titleMap: {
					add: '新增',
					edit: '编辑',
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
                    sort:99,
                    status:"1",
                    max_num:2
                },
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入桌号'}
					],
				}
			}
		},
		mounted() {
            this.getcat()  	
		},
		methods: {
            async getcat(){
				var res = await this.$API.system.universal.list.get({},'/desk/area');
				if(res.code == 200){
                        var arr = res.data.rows
                        this.cat =arr
                        this.cat.unshift({'name':'无','value':'','id':''})	 
				} 
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						delete this.form.pname;
						this.isSaveing = true;
                        var res = await this.$API.system.universal.handle.post(this.form,'/desk/desk_save');
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form = data
			}
		}
	}
</script>

<style>
</style>
